<template>
  <div class="Terminal_box" ref="boxTheSize">
    <el-row>
      <el-col :span="24">
        <div class="grid-content bg-purple-light">
          <div class="tltle_box">
            <el-row :gutter="24" style="width: 100%;">
            <el-col :sm="24" :md="24" :lg="14">
            <div class="tltle_box_l" style="padding: 5px;">
              <div class="Material_r">
                <el-input placeholder="请输入关键字" v-model="inputText" @clear="seektableList" clearable></el-input>
                <el-button type="primary" class="button1" icon="el-icon-search" @click="seektableList">搜索</el-button>
                <el-button type="primary" class="button1" icon="el-icon-search" @click="seektableList2">未播放节目</el-button>
                <!-- <el-button class="refresh" @click="AdvancedSearch=true">高级搜索</el-button> -->
                <!-- <el-tooltip class="item button2"  effect="dark" content="数据备份" placement="top"> -->
               <!--  <el-button @click="DataBackup=true" class="button2" style="padding: 0px 5px;">
                  <img src="../../assets/images/paly2_03.png" alt />
                  <div>数据备份</div>
                </el-button> -->
              <!-- </el-tooltip> -->
              </div>
               
            </div>
            </el-col>
            <el-col :sm="24" :md="24" :lg="10">
            <div class="tltle_box_r">
              <el-date-picker
                v-model="tiemValue"
                type="datetimerange"
                range-separator="至"
                @change="seektableList"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :clearable="false"
                value-format="yyyy-MM-dd HH:mm:ss"
                :default-time="['00:00:00', '23:59:59']"
              ></el-date-picker>
            </div>
             </el-col>
            </el-row>
          </div>
          <div class="Terminal_table">
            <zycm-table
              @zyLoadData="loadData"
              zy-table-ref="media_group_palylog_table"
              :zy-table-data="tabledataList"
              zy-table-height="63vh"
            >
              <el-table-column label="素材名称" prop="materialName" min-width="100" show-overflow-tooltip></el-table-column>
              <el-table-column label="播放总次数" prop="sumTime" width="100" show-overflow-tooltip></el-table-column>
              <el-table-column label="操作" width="100">
                <template slot-scope="scope">
                  <el-button type="primary" :size="GlobalVar.sizeSet.btn" v-if="Invoking.forFunctionCode('playLog:selectPagePlayTimeDetail')" @click="OpenLogDetails(scope.row)">查看</el-button>
                </template>
              </el-table-column>
            </zycm-table>
          </div>
        </div>
      </el-col>
    </el-row>
    <!-- 高级搜索 -->
    <el-dialog
      title="高级搜索"
      :visible.sync="AdvancedSearch"
      :width="this.$store.getters.offsetWidth"
      v-dialogDrag
      ref="dialog__wrapper"
      :close-on-click-modal="false"
    >
      <div class="dialog-body">
        <div class="line">
          <div class="Advanced_head">
            <div>
              <el-select placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </div>
            <div>
              <el-input></el-input>
            </div>
          </div>
          <div>
            <el-form ref="form" label-width="100px">
              <el-form-item label="时间范围">
                <el-select placeholder="请选择时间范围">
                  <el-option label="区域一" value="shanghai"></el-option>
                  <el-option label="区域二" value="beijing"></el-option>
                </el-select>
              </el-form-item>
            </el-form>
          </div>
          <span
            style="width: 100%;text-align: center;margin: 20px auto 0;height: 50px;display: block;"
            class="button_dialog"
          >
            <el-button style="background-color:#4d9ce0;color:#fff">查 询</el-button>
            <el-button @click="AdvancedSearch=false">关 闭</el-button>
          </span>
        </div>
      </div>
    </el-dialog>
    <!-- 日志详情 -->
    <el-dialog
      :title="material.materialName?material.materialName+'--日志详情':'日志详情'"
      :visible.sync="LogDetails"
      :width="this.$store.getters.offsetWidth"
      class="LogDetails"
      v-dialogDrag
      ref="dialog__wrapper"
      :close-on-click-modal="false"
      @close="closeLogDetails"
    >
      <div class="dialog-body">
        <div class="line">
            <zycm-table
              @zyLoadData="loadData"
              zy-table-ref="media_group_palylog_LogDetails"
              :zy-table-data="tabledataList"
              zy-table-height="450px"
              zy-border
            >
            <el-table-column label="终端名称" prop="clientName" min-width="100" show-overflow-tooltip></el-table-column>
            <el-table-column label="终端组名称" prop="mediaGroupName" min-width="100" show-overflow-tooltip></el-table-column>
            <el-table-column label="素材名称" prop="materialName" min-width="100" show-overflow-tooltip></el-table-column>
            <el-table-column label="时间" prop="logDate" width="140" show-overflow-tooltip>
              <template slot-scope="scope">
                <div>{{scope.row.logDate | dateFormat}}</div>
              </template>
            </el-table-column>
            <el-table-column label="播放次数" prop="playTime" min-width="100" show-overflow-tooltip></el-table-column>
            </zycm-table>
        </div>
      </div>
    </el-dialog>
    <!-- 数据备份 -->
    <el-dialog
      title="数据备份"
      :visible.sync="DataBackup"
      :width="this.$store.getters.offsetWidth"
      v-dialogDrag
      ref="dialog__wrapper"
      :close-on-click-modal="false"
    >
      <div class="dialog-body">
        <div class="line">
          <el-form ref="form" label-width="120px">
            <el-form-item label="选择备份区间">
              <el-select placeholder="请选择日志">
                <el-option label="区域一" value="shanghai"></el-option>
                <el-option label="区域二" value="beijing"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <span
            style="width: 100%;text-align: center;margin: 0 auto;height: 50px;display: block;"
            class="button_dialog"
          >
            <el-button style="background-color:#4d9ce0;color:#fff">开始备份</el-button>
            <!-- <el-button>取 消</el-button> -->
          </span>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      query: {
        page: 1,
        pageSize: this.GlobalVar.query.pageSize,
        total: 0,
      },
      query1: {
        page: 1,
        pageSize: this.GlobalVar.query.pageSize,
        total: 0,
      },
      AdvancedSearch: false,
      LogDetails: false,
      DataBackup: false,
      // 搜索字段
      inputText: "",
      TerminalHeight: "",
      tableData: [],
      tiemValue: [],
      options: [
        {
          value: "选项1",
          label: "黄金糕",
        },
      ],
      PalyLogTableList: [],
      materialId: "",
      material: "",
      thisFormData: new FormData(),
      tabledataList: [], //保存当页的所有table数据
      tablePageList: [], //保存对应table的page数据
      searchType:1,
    };
  },
  created() {
    this.clientHeight();
    this.tiemValue.push(
      this.dateFormat(new Date()).substring(0, 10) + " 00:00:00"
    );
    this.tiemValue.push(
      this.dateFormat(new Date()).substring(0, 10) + " 23:59:59"
    );
     let fd = new FormData();
    fd.set("startDate", this.tiemValue[0]);
    fd.set("endDate", this.tiemValue[1]);
    this.currentTableLoad("media_group_palylog_table", true,fd);
    // this.tableList();
  },
  watch: {
    tiemValue(value) {
      if (!value) {
        this.tiemValue = [];
        this.tiemValue.push(
          this.dateFormat(new Date()).substring(0, 10) + " 00:00:00"
        );
        this.tiemValue.push(
          this.dateFormat(new Date()).substring(0, 10) + " 23:59:59"
        );
        this.tableList();
      }
    },
  },
  methods: {
                  //给子组件刷新用
    async loadData(formData, tableRef) {
      //提前修正isReqData状态 后续赋值随时会触发table组件的computed  后面还要进行一次修正
      for (let key in this.tabledataList) {
        if (this.tabledataList[key].ref === tableRef) {
          this.tabledataList[key].isReqData = true; //标记更新数据的状态 组件中会根据此判断是否重新更新数据
        } else {
          this.tabledataList[key].isReqData = false;
        }
      }

      //子组件触发查询时  处理查询的参数
      if (this.tablePageList[tableRef]) {
        this.tablePageList[tableRef].set("page", formData.get("page"));
        this.tablePageList[tableRef].set("pageSize", formData.get("pageSize"));
      }

      let res;
      if (tableRef == "media_group_palylog_table") {
        //发布-选择节目列表
        res = await this.$http.post(
          "playLog/pageStatisticsPlayTime",
          this.tablePageList[tableRef]
        );
      }else if (tableRef == "media_group_palylog_LogDetails") {
        res = await this.$http.post(
          "playLog/selectPagePlayTimeDetail",
          this.tablePageList[tableRef]
        );
      }

      if (res && res.data.result === "success") {
        //记录更新分页数据
        res.data.ref = tableRef;
        //更新记录
        let sign = 0;
        for (let key in this.tabledataList) {
          if (this.tabledataList[key].ref === tableRef) {
            if (sign > 1) {
              //去掉重复数据
              this.tabledataList.splice(key, 1);
            } else {
              this.tabledataList.splice(key, 1, res.data);
              this.tabledataList[key].isReqData = true; //标记更新数据的状态 组件中会根据此判断是否重新更新数据
            }
            sign++;
          } else {
            this.tabledataList[key].isReqData = false;
          }
        }
        if (sign == 0) {
          //没有初始化时
          res.data.isReqData = true;
          this.tabledataList.push(res.data);
        }
      } else {
        if (res && res.data.result === "error") {
          this.$message({
            type: "error",
            message: res.data.message,
          });
        } else {
          this.$message({
            type: "error",
            message: "请求数据出现异常",
          });
        }
      }
    },
    //当页刷新数据
    currentTableLoad(tableRef, firstPage, fd) {
      if (firstPage) {
        //加载第一页数据
        if (!this.tablePageList[tableRef]) {
          this.tablePageList[tableRef] = new FormData();
          this.tablePageList[tableRef].set("pageSize", this.GlobalVar.query.pageSize);
        }
        this.tablePageList[tableRef].set("page", 1);
      }
      //如果有其他查询参数 合并处理
      if (fd) {
        for (let item of fd.entries()) {
          this.tablePageList[tableRef].set(item[0], item[1]);
        }
      }
      this.loadData(this.tablePageList[tableRef], tableRef);
    },

    seektableList() {
      this.searchType=1;
      this.tableList();
    },
    seektableList2() {
      this.inputText = "";
      this.searchType=2;
      this.tableList();
    },
    closeLogDetails() {
      this.PalyLogTableList = [];
      this.query1.page = 1;
      this.query1.pageSize = this.GlobalVar.query.pageSize;
    },
    indexMethod(index) {
      return (this.query.page - 1) * this.query.pageSize + index + 1;
    },
    async tableList() {
      let formData = new FormData();
      let token = window.sessionStorage.getItem("token");
      formData.set("zycmToken", token);
      formData.set("materialName", this.inputText);
      formData.set("page", !this.query.page ? 1 : this.query.page);
      formData.set(
        "pageSize",
        !this.query.pageSize ? this.GlobalVar.query.pageSize : this.query.pageSize
      );
      formData.set(
        "startDate",
        this.tiemValue[0] ? this.tiemValue[0] : ""
      );
      formData.set(
        "endDate",
        this.tiemValue[1] ? this.tiemValue[1] : ""
      );
      if(this.searchType === 2){
        formData.set("noPlay", "noPlay");
      }else{
        formData.set("noPlay", "");
      }
     this.currentTableLoad("media_group_palylog_table", true,formData);
    },
    async OpenLogDetails(e) {
      if (e) {
        this.materialId = e.materialId;
        this.material = e;
      }
      let formData = new FormData();
      let token = window.sessionStorage.getItem("token");
      formData.set("zycmToken", token);
      formData.set("startDate", this.tiemValue[0] ? this.tiemValue[0] : "");
      formData.set("endDate", this.tiemValue[1] ? this.tiemValue[1] : "");
      formData.set("materialId", this.materialId);
      if(this.searchType === 2){
        formData.set("noPlay", "noPlay");
      }else{
        formData.set("noPlay", "");
      }
      this.currentTableLoad("media_group_palylog_LogDetails", true,formData);
      this.LogDetails = true;
    },
    clientHeight() {
      this.$nextTick(() => {
        this.TerminalHeight = this.$refs.boxTheSize.clientHeight - 95;
      });
    },
    // 转换时间
    dateFormat(time) {
      let date = new Date(time + 8 * 3600000);
      let str = date.toISOString().replace("T", " ");
      return str.substr(0, str.lastIndexOf("."));
    },
  },
};
</script>



<style lang="stylus" scoped>
/deep/.el-form-item__label{
    margin-top: 1px;
}
.Terminal_box {
    overflow: hidden;
  .auditOpinion {
    /deep/.el-form-item__label {
      line-height: 80px;
    }
    /deep/.el-form-item__label,
    /deep/.el-textarea__inner {
      height: 80px;
    }
  }
  .BgColor {
    background-color: #e4f2ff;
    height: 100%;
  }
  /deep/.has-gutter .el-checkbox {
    display: none;
  }
  .tltle_box {
    margin: 1.5% 0;
    background-color: #fff;
    display: flex;
    justify-content: space-between;
    // height: 100px;
    padding 1%
    width: 100%;
    align-items: center;
    margin-bottom: 15px;
    border-radius: 5px;
    .tltle_box_l {
      display: flex;
      .Material_r {
        display: flex;
        // margin-left: 20px;
        .button1 {
          margin: 0 0 0 20px;
          height: 40px;
          background-color: #2183d8;
        }
        .button2 {
        // float: right;
        vertical-align: middle;
        background-color: #f2faff;
        height: 50px;
        border: none;
        margin-left: 0;
        margin: 0 20px;
        padding: 0 5px;
        border-radius: 5px;
        font-size:12px;
        img {
          width: 26px;
          height: 26px;
        }
      }
        .refresh {
          background-color: #fff;
          color: #2183d8;
        }
      }
    }
    .tltle_box_r {
      padding: 5px;
      //   height: 50px;
      /deep/.el-date-picker {
        margin-bottom: 10px;
      }
      .el-button {
        // float: right;
        vertical-align: middle;
        background-color: #f2faff;
        width: 50px;
        height: 50px;
        border: none;
        margin-left: 0;
        margin: 0 20px;
        img {
          width: 26px;
          height: 26px;
        }
      }
    }
    /deep/.el-table th,
    .el-table tr {
      background-color: #fafafa;
    }
  }
  .Terminal_table {
    margin:1.5% 0%;
    width: 100%;
    background-color: #fff;
    border: 0.1px solid #fff;
    .el-table {
      margin: 1.5%;
      width: 97%;
    }
  }
  .Terminal_tree {
    padding: 20px;
  }
  /deep/.el-tree {
    background-color: #e4f2ff;
  }
  .pagination {
    display: flex;
    height: 60px;
    justify-content: center;
  }
  /deep/.el-pager li,
  /deep/.el-pagination .btn-next,
  /deep/ .el-pagination .btn-prev {
    border: 1px solid #eee;
    border-radius: 5px;
    margin: 0 5px;
    color: #333;
    padding: 0;
  }
  /deep/.el-pager li.active {
    background-color: #2183d8;
    color: #fff;
  }
  /deep/.el-pagination__sizes {
    padding-left: 20px;
  }
  .fold {
    display: flex;
    width: 100%;
  }
  .fold /deep/.el-form-item__label {
    color: #0095fb;
  }
  .fold /deep/.el-form-item {
    width: 25%;
  }
  /deep/.el-tabs--bottom /deep/.el-tabs__item.is-bottom:nth-child(2),
  /deep/.el-tabs--bottom .el-tabs__item.is-top:nth-child(2),
  /deep/.el-tabs--top .el-tabs__item.is-bottom:nth-child(2),
  /deep/.el-tabs--top .el-tabs__item.is-top:nth-child(2) {
    padding-left: 20px;
  }
  /deep/.el-tabs__item {
    padding: 0 20px;
  }
  /deep/.el-tabs__active-bar {
    padding: 0 20px;
    left: -20px;
    color: #2183d8;
  }
  /deep/.el-table th,
  .el-table tr {
    background-color: #fafafa;
  }
}
.LogDetails {
  /deep/.el-dialog__body {
    padding: 10px 20px;
  }
}
/deep/.el-dialog {
  .Advanced_head {
    overflow: hidden;
    margin: 0 0 20px;
    div:nth-child(1) {
      float: left;
      width: 250px;
    }
    div:nth-child(2) {
      float: right;
      width: 250px;
    }
  }
  .inputTransparent {
    /deep/.el-form-item__content {
      display: flex;
      // justify-content: space-around
      text-align: center;
    }
  }
  .first_box {
    height: 250px;
    width: 99%;
    border: 1px solid #eee;
  }
  .auditOpinion {
    /deep/.el-form-item__label {
      line-height: 120px;
    }
    /deep/.el-form-item__label,
    /deep/.el-textarea__inner {
      height: 120px;
    }
  }

  /deep/.el-tree {
    background-color: #fff;
    padding: 0 0 5% 10%;
  }
  .selectTerminal {
    /deep/.el-tree {
      background-color: #fff;
      padding: 0 0 5% 10%;
    }
  }
  /deep/.el-select {
    display: block;
  }
  /deep/.el-dialog__header {
    background-color: #efefef;
    padding: 0;
    line-height: 50px;
    padding-left: 20px;
    color: #333;
    font-size: 16px;
  }
  /deep/.el-form-item__label {
    background-color: #efefef;
    border-radius: 0;
    height: 40px;
    border: 1px solid #dcdfe6;
    border-right: none;
    box-sizing: border-box;
    text-align: center;
    padding-left: 10px;
  }
  /deep/.el-input__inner,
  /deep/.el-textarea__inner {
    border-radius: 0;
  }
  /deep/.el-button {
    padding: 12px 25px;
  }
  .opinion {
    /deep/.el-form-item__label {
      height: 80px;
      line-height: 80px;
    }
  }
}
</style>
